package com.tlgen.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.tlgen.R;
import com.tlgen.bean.Credit;
import com.xuexiang.xui.widget.layout.ExpandableLayout;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;

import java.util.ArrayList;

public class RebackCodeAdapter extends RecyclerView.Adapter<RebackCodeAdapter.myViewHodler> {

    private Context context;
    private ArrayList<Credit> creditArrayList;

    //构造方法，传入数据
    public RebackCodeAdapter(Context context, ArrayList<Credit> creditArrayList) {
        this.context = context;
        this.creditArrayList = creditArrayList;
    }

    @Override
    public RebackCodeAdapter.myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.activity_rebackcode_item, null);
        return new RebackCodeAdapter.myViewHodler(itemView);
    }

    @Override
    public void onBindViewHolder(RebackCodeAdapter.myViewHodler holder, int position) {

        holder.mExpandableLayout.setOnExpansionChangedListener((expansion, state) -> {
            if (holder.stvExpandable != null && holder.stvExpandable.getRightIconIV() != null) {
                holder.stvExpandable.getRightIconIV().setRotation(expansion * 90);
            }
        });
        holder.stvExpandable.setOnSuperTextViewClickListener(superTextView -> {
            if (holder.mExpandableLayout != null) {
                holder.mExpandableLayout.toggle();
            }
        });

        Credit credit = creditArrayList.get(position);
        if (!"".equals(credit.getRebackCode()) || null != credit.getRebackCode()) {
            holder.stvExpandable.setCenterString("点击查看 " + credit.getDuringYear() + " 年度校验码");
            holder.tvContent.setText(credit.getRebackCode());
        }
    }

    @Override
    public int getItemCount() {
        return creditArrayList.size();
    }

    class myViewHodler extends RecyclerView.ViewHolder {
        SuperTextView stvExpandable;
        ExpandableLayout mExpandableLayout;

        TextView tvContent;

        public myViewHodler(View itemView) {
            super(itemView);
            stvExpandable = itemView.findViewById(R.id.stv_expandable);
            mExpandableLayout = itemView.findViewById(R.id.expandable_layout);

            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}
